package org.extreme.pivot.grid;

import java.awt.Rectangle;

import org.apache.pivot.util.ListenerList;


public class GridSelection {
	private Grid grid = null;
	private GridSelectionChangeEvent eventObject;
	private Rectangle editSelection = new Rectangle(0, 0, 1, 1);
	private SelectionChangeListenerList selectionChangeListenerList = new SelectionChangeListenerList();

	public GridSelection(Grid grid) {
		this.grid = grid;
	}
	
	public void setEditSelection(Rectangle editSelection) {
		this.editSelection = editSelection;
		
		fireSelectionChangeListeners();
	}

	public Rectangle getEditSelection() {
		return editSelection;
	}
	
	public void arrowLeft() {
		if (editSelection.x > 0) {
			editSelection.x --;
		}
		
		fireSelectionChangeListeners();
	}
	
	public void arrowRight() {
		editSelection.x ++;
		
		fireSelectionChangeListeners();
	}
	
	public void arrowUp() {
		if (editSelection.y > 0) {
			editSelection.y --;
		}
		
		fireSelectionChangeListeners();
	}
	
	public void arrowDown() {
		editSelection.y ++;
		
		fireSelectionChangeListeners();
	}
	
	public SelectionChangeListenerList getSelectionChangeListenerList() {
		return selectionChangeListenerList;
	}
	
	private void fireSelectionChangeListeners() {
		if (eventObject == null) {
			eventObject = new GridSelectionChangeEvent(this.grid);
		}
		
		selectionChangeListenerList.selectionChanged(eventObject);
	}
	
	public static class SelectionChangeListenerList extends ListenerList<SelectinChangeListener> 
								implements SelectinChangeListener {

		@Override
		public void selectionChanged(GridSelectionChangeEvent e) {
			for (SelectinChangeListener listener : this) {
				listener.selectionChanged(e);
			}
		}
		
	}
}
